//
// MaFenetre.cpp for 210random in /home/faucon_l//Work/Tek2/projet/maths/210random
//
// Made by leo faucon
// Login   <faucon_l@epitech.net>
//
// Started on  Wed Jun 15 15:41:50 2011 leo faucon
// Last update Mon Jun 20 00:24:29 2011 leo faucon
//

#include <iostream>
#include "Maths.hpp"
#include "convert.hpp"
#include "MaFenetre.hpp"

MaFenetre::MaFenetre(QApplication &app)
{

  setFixedSize(600, 400);

  QGridLayout	*layout = new QGridLayout;

  // 1 : Créer le QTabWidget
  QTabWidget *onglets = new QTabWidget(this);
  onglets->setGeometry(30, 20, 500, 300);

  // 2 : Créer les pages, en utilisant un widget parent pour contenir chacune des pages
  QWidget *page1 = new QWidget;
  QWidget *page2 = new QWidget;

  // 3 : Créer le contenu des pages de widgets

  // Page 1

  borne_a1	= new QLineEdit("0");
  borne_b1	= new QLineEdit("10");
  nb1		= new QLineEdit("1000");
  calculer1	= new QPushButton("Calculer");
  resultat1	= new QLabel("Resultat : ");

  QGridLayout *vbox1 = new QGridLayout;
  vbox1->addWidget(new QLabel("Borne min :"),0, 0);
  vbox1->addWidget(borne_a1,0,1,1,2);
  vbox1->addWidget(new QLabel("Borne max :"),1,0);
  vbox1->addWidget(borne_b1,1,1,1,2);
  vbox1->addWidget(new QLabel("Nombre random:"),2,0);
  vbox1->addWidget(nb1,2,1,1,2);
  vbox1->addWidget(resultat1,3,0,1,3);
  vbox1->addWidget(calculer1,4,1);
  page1->setLayout(vbox1);

  // Page 2

  calculer2	= new QPushButton("Valider");
  charger	= new QPushButton("Charger");
  //  QVBoxLayout *vbox2 = new QVBoxLayout;

  QGridLayout	*vbox2 = new QGridLayout;

  vbox2->addWidget(calculer2,0,0);
  vbox2->addWidget(charger,0,1);

  page2->setLayout(vbox2);

  // 4 : ajouter les onglets au QTabWidget, en indiquant la page qu'ils contiennent
  onglets->addTab(page1, "Exemple 1");
  onglets->addTab(page2, "Exemple 2");

  Quit = new QPushButton("Quitter");

  layout->addWidget(onglets, 0, 0, 1, 3);
  layout->addWidget(Quit, 1, 1);

  setLayout(layout);

  QWidget::connect(Quit, SIGNAL(clicked()), &app, SLOT(quit()));
  QWidget::connect(calculer1, SIGNAL(clicked()), this, SLOT(calc1()));

}

int	MaFenetre::check(std::string str)
{
  for (size_t i = 0; i < str.size(); i++)
    {
      if (str[i] < '0' || str[i] > '9')
	{
	  QMessageBox::critical(this, "Erreur", "Saisie d'un\nCaractere interdit");
	  return (1);
	}
    }
  return (0);
}

void	MaFenetre::calc1()
{

  if (!borne_a1->displayText().isEmpty() && !borne_b1->displayText().isEmpty()
      && !nb1->displayText().isEmpty())
    {
      if (check(borne_a1->displayText().toStdString()))
	return;
      if (check(borne_b1->displayText().toStdString()))
	return;
      if (check(nb1->displayText().toStdString()))
	return;
      int	borne_a;
      int	borne_b;
      int	nb;

      borne_b = string_to_var<int>(borne_b1->displayText().toStdString());
      if (borne_b > 30)
	{
	  QMessageBox::critical(this, "Erreur", "Valeur borne max\n incorrect");
	  return;
	}
      borne_a = string_to_var<int>(borne_a1->displayText().toStdString());
      if (borne_a < 0 || borne_a > borne_b)
	{
	  QMessageBox::critical(this, "Erreur", "valeur borne min\n incorrect");
	  return;
	}
      nb = string_to_var<int>(nb1->displayText().toStdString());
      if (nb > 100000 || nb <= 0)
	{
	  QMessageBox::critical(this, "Erreur", "Valeur nombre random\n incorrect");
	  return;
	}
      Maths	*math = new Maths;

      if (!math->readfile())
	QMessageBox::critical(this, "Erreur", "Lecture table khi-deux");
      else
	resultat1->setText(math->calc(borne_a, borne_b, nb).c_str());
      delete math;
    }
}
